package com.tengju.payment.infrastructure.persistence.mybatis.mapper;


import com.tengju.payment.infrastructure.persistence.mybatis.dataobject.TradeRecordDO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface TradeRecordMapper {

    String DEFAULT_COLUMN_LIST =
            "id,business_type,channel_id,channel_type,status,business_no,trade_no,out_trade_no,trade_amount,request_time,complete_time,request_info,response_info,extra,create_time,update_time,deleted";

    String DEFAULT_INSERT_COLUMN_LIST =
            "id,business_type,channel_id,channel_type,status,business_no,trade_no,out_trade_no,trade_amount,request_time,complete_time,request_info,response_info,extra";

    /**
     * 插入数据
     */
    @Insert("insert INTO pay_trade_record (" + DEFAULT_INSERT_COLUMN_LIST +
            ") VALUES(#{id}, #{businessType}, #{channelId},#{channelType}, #{status},#{businessNo}, #{tradeNo}, #{outTradeNo}, #{tradeAmount}, #{requestTime}," +
            "#{completeTime},#{requestInfo},#{responseInfo},#{extra}) ")
    @Options(keyProperty = "id")
    long insert(TradeRecordDO tradeRecordDO);


    /**
     * 修改状态
     */
    @Update(" UPDATE pay_trade_record SET status = #{afterStatus}" +
            " WHERE id = #{id} AND status = #{beforeStatus} LIMIT 1")
    int updateStatus(@Param("id") long id,
                     @Param("beforeStatus") int beforeStatus,
                     @Param("afterStatus") int afterStatus);

    /**
     * 修改转账信息
     */
    @Update(" UPDATE pay_trade_record SET status = #{tradeRecordDO.status}," +
            "channel_id = #{tradeRecordDO.channelId},channel_type = #{tradeRecordDO.channelType}," +
            "trade_no = #{tradeRecordDO.tradeNo},out_trade_no = #{tradeRecordDO.outTradeNo}," +
            "request_time = #{tradeRecordDO.requestTime},complete_time = #{tradeRecordDO.completeTime}," +
            "extra = #{tradeRecordDO.extra} " +
            " WHERE id = #{tradeRecordDO.id} AND status = #{beforeStatus} LIMIT 1")
    int updateTransfer(@Param("beforeStatus") int beforeStatus,
                     @Param("tradeRecordDO") TradeRecordDO tradeRecordDO);


    /**
     * 根据主键id获取
     * @return ss
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_trade_record " +
            " WHERE id = #{id}  " +
            " and deleted = 0 limit 1</script>")
    @Results(id = "tradeRecord",
            value = {
                    @Result(property = "id", column = "id", javaType = Long.class),
                    @Result(property = "businessType", column = "business_type", javaType = Integer.class),
                    @Result(property = "channelId", column = "channel_id", javaType = Integer.class),
                    @Result(property = "channelType", column = "channel_type", javaType = Integer.class),
                    @Result(property = "status", column = "status", javaType = Integer.class),
                    @Result(property = "businessNo", column = "business_no", javaType = String.class),
                    @Result(property = "tradeNo", column = "trade_no", javaType = String.class),
                    @Result(property = "outTradeNo", column = "out_trade_no", javaType = String.class),
                    @Result(property = "tradeAmount", column = "trade_amount", javaType = Integer.class),
                    @Result(property = "requestTime", column = "request_time", javaType = LocalDateTime.class),
                    @Result(property = "completeTime", column = "complete_time", javaType = LocalDateTime.class),
                    @Result(property = "requestInfo", column = "request_info", javaType = String.class),
                    @Result(property = "responseInfo", column = "response_info", javaType = String.class),
                    @Result(property = "createTime", column = "create_time", javaType = LocalDateTime.class),
                    @Result(property = "updateTime", column = "update_time", javaType = LocalDateTime.class),
                    @Result(property = "deleted", column = "deleted", javaType = Boolean.class),
            })
    TradeRecordDO getById(@Param("id") long id);


    /**
     * 根据业务流水号 业务类型查询
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_trade_record " +
            " WHERE business_no = #{businessNo} and business_type = #{businessType}  " +
            " and deleted = 0 limit 1</script>")
    @ResultMap("tradeRecord")
    TradeRecordDO getByBusinessNoType(@Param("businessNo") String businessNo,@Param("businessType") int businessType);


    /**
     * 根据状态查询
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_trade_record " +
            " WHERE status = #{tradeStatus} " +
            " and deleted = 0 and create_time &lt; SUBDATE(NOW(),interval 3 MINUTE) </script>")
    @ResultMap("tradeRecord")
    List<TradeRecordDO> getByTradeStatus(@Param("tradeStatus") int tradeStatus);


    /**
     * 根据状态查询
     */
    @Select(" <script>SELECT " + DEFAULT_COLUMN_LIST + " FROM pay_trade_record " +
            " WHERE trade_no = #{tradeNo} " +
            " and deleted = 0 </script>")
    @ResultMap("tradeRecord")
    TradeRecordDO getByTradeNo(@Param("tradeNo") String tradeNo);
}
